from django.shortcuts import render
from django.views import View
from rest_framework.viewsets import ModelViewSet

from apps.index.models import Category
from apps.index.serializers import CategoriesSerializer
from apps.index.utils import get_user
from apps.users.models import User


class IndexView(View):
    def get(self, request):
        # 用户验证
        username = get_user(request)
        try:
            user = User.objects.get(username=username)
            context = {
                "username": user.username
            }
        except:
            context = None
        return render(request, "index.html", context=context)


class IndexViewSet(ModelViewSet):
    """主页"""
    # 指定序列化器类
    serializer_class = CategoriesSerializer
    queryset = Category.objects.filter(parent__isnull=True)
